<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>广告管理</title>
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	var url;
	//查询员工
	function searchAdvert() {
		$("#dg").datagrid('load', {
			"account" : $("#c_account").val(),
			"type" : $("#c_type").val()
		});
	}
	//添加员工
	function openAddAdvertDialog() {
		resetValue();
		$("#advertForm").dialog("open").dialog("setTitle", "添加广告");
		url = "${pageContext.request.contextPath}/advert/save.do";
	}
	//保存员工信息到后台
	function saveAdvert() {
		$("#fm").form("submit", {
			url : url,
			onSubmit : function() {
				return $(this).form("validate");
			},
			success : function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					$.messager.alert("系统提示", "保存成功！");
					resetValue();
					$("#advertForm").dialog("close");
					$("#dg").datagrid("reload");
				} else {
					$.messager.alert("系统提示", "保存失败！");
					return;
				}
			}
		});
	}
	//更新员工信息
	function openUpdateAdvertDialog() {
		var selectedRows = $("#dg").datagrid("getSelections");
		if (selectedRows.length != 1) {
			$.messager.alert("系统提示", "请选择一个要修改的广告！");
			return;
		}
		var row = selectedRows[0];
		$("#advertForm").dialog("open").dialog("setTitle", "修改广告信息");
		$("#fm").form("load", row);
		url = "${pageContext.request.contextPath}/advert/save.do?id=" + row.id;
	}

	//重置对话框数据为空
	function resetValue() {
		$("#account").val("");
		$("#url").val("");
		$("#type").val("");
	}
	//关闭对话框
	function closeAdvertDialog() {
		$("#advertForm").dialog("close");
		resetValue();
	}
	//删除员工
	function deleteAdvert() {
		var selectRows = $("#dg").datagrid("getSelections");
		if (selectRows.length == 0) {
			$.messager.alert("系统提示", "请选择需要删除的行数据！");
			return;
		}
		var strIds = [];
		for (var i = 0; i < selectRows.length; i++) {
			strIds.push(selectRows[i].id);
		}
		var ids = strIds.join(",");
		$.messager.confirm("系统提示", "您确认要删除这<font color=red>"
				+ selectRows.length + "</font>条数据吗？", function(r) {
			if (r) {
				$.post("${pageContext.request.contextPath}/advert/delete.do", {
					ids : ids
				}, function(result) {
					if (result.success) {
						$.messager.alert("系统提示", "数据已成功删除！");
						$("#dg").datagrid("reload");
					} else {
						$.messager.alert("系统提示", "删除失败,请联系管理员！");
					}
				}, "json");
			}
		});
	}

	function AdvertTypeFormart(val, row) {
		if (val != null) {
			if (val == 1) {
				return "图片";
			} else if (val == 0) {
				return "视频";
			}
		}
	}

	function AdvertUrlFormart(val, row) {
		if (val != null) {
			if (row.type == 1) {
				return "<img src='"+val+"' style='width: 100px ;height: 80px' />";
			} else if (row.type == 0) {
				return "<video src='"+val+"' width: 60px height: 40px  controls='controls'></video>";
			}
		}
	}
</script>
</head>
<body style="margin: 1px">
	<table id="dg" title="广告管理" class="easyui-datagrid" fitColumns="true"
		pagination="true" rownumbers="true"
		url="${pageContext.request.contextPath}/advert/list.do" fit="true"
		toolbar="#tb">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="id" width="20" align="center">广告编号</th>
				<th field="account" width="30" align="center">广告描述</th>
				<th field="url" formatter="AdvertUrlFormart" width="30"
					align="center">广告预览</th>
				<th field="type" formatter="AdvertTypeFormart" width="30"
					align="center">广告类型</th>
			</tr>
		</thead>
	</table>
	<div id="tb">
		<div>
			<a href="javascript:openAddAdvertDialog()" class="easyui-linkbutton"
				iconCls="icon-add" plain="true">添加</a>&nbsp;&nbsp;&nbsp;<a
				href="javascript:deleteAdvert()" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true">删除</a>
		</div>
		<div>
			&nbsp;广告名称:&nbsp;<input type="text" id="c_account" size="12"
				onkeydown="if(event.keyCode==13) searchAdvert()" />
			&nbsp;&nbsp;&nbsp;广告类型:&nbsp;<input type="text" id="c_type" size="12"
				onkeydown="if(event.keyCode==13) searchAdvert()" />&nbsp;&nbsp;&nbsp;
			<a href="javascript:searchAdvert()" class="easyui-linkbutton"
				iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>
	<div id="advertForm" class="easyui-dialog"
		style="width: 500px; height: 480px; padding: 10px 20px" closed="true"
		buttons="#dlg-buttons">
		<form id="fm" method="post" enctype="multipart/form-data">
			<table cellspacing="8px">
				<tr>
					<td>广告名称:</td>
					<td><input type="text" id="account" name="account"
						class="easyui-validatebox" required="true" /></td>
				</tr>
				<tr>
					<td>广告类型</td>
					<td>图片<input id="ad_type" value="1" name="type"
						checked="checked" type="radio"> 视频<input name="type"
						value="0" type="radio">
					</td>
				</tr>
				<tr>
					<td>上传广告</td>
					<td><input name="upload" value="0" type="file"></td>
				</tr>


			</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:saveAdvert()" class="easyui-linkbutton"
			iconCls="icon-ok">保存</a> <a href="javascript:closeAdvertDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
</body>
</html>